﻿using DeHeng_Mes.Model.PlcModel.Left;
using System;
using System.Collections.Generic;
using DeHeng_Mes.Model.PlcModel.Right;
using DeHeng_Mes.Constant;
using System.Management;
using DeHeng_Mes.Model;
using DeHeng_Mes.Model.Table;
using DeHeng_Mes.Model.PlcModel.EndWorkSpr;
using DeHeng_Mes.Model.PlcModel.RightSpr;
using DeHeng_Mes.Model.PlcModel.RightGlue;
using DeHeng_Mes.Model.PlcModel.StudWeld;
using S7.Net;
using DeHeng_Mes.Model.PlcModel.Sub.Clinch01;
using DeHeng_Mes.Model.PlcModel.Sub.Clinch02;
using DeHeng_Mes.Model.PlcModel.Sub.Clinch03;
using DeHeng_Mes.Model.PlcModel.Sub.Clinch04;
using DeHeng_Mes.Model.PlcModel.Sub.Clinch05;
using DeHeng_Mes.Model.PlcModel.Sub.FDS01;
using DeHeng_Mes.Model.PlcModel.Sub.Glue01;
using DeHeng_Mes.Model.PlcModel.Sub.Spr01;
using DeHeng_Mes.Model.PlcModel.Sub.Spr02;
using DeHeng_Mes.Model.PlcModel.Sub.StudWeld01;
using DeHeng_Mes.Model.PlcModel.Sub.StudWeld02;

namespace DeHeng_Mes.Utils
{
    public static class MappingTable
    {
        /// <summary>
        /// plc数据与实体类的映射
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static Dictionary<object, Type> PlcMapping(Plc plc, string plcName)
        {
            // 初始化返回值
            Dictionary<object, Type> dic = new Dictionary<object, Type>();

            // 定义一个类型和地址的映射表
            var typeAddressMap = new Dictionary<string, List<(Type type, int address)>>
            {
                { "leftSpr", new List<(Type, int)>
                    {
                        (typeof(LeftSprBeat), SystemConstant.DB6001),
                        (typeof(LeftSprCode), SystemConstant.DB6009),
                        (typeof(LeftSprDeviceErrorMessage), SystemConstant.DB6006),
                        (typeof(LeftSprDeviceErrorStatus), SystemConstant.DB6010),
                        (typeof(LeftSprPower), SystemConstant.DB6003),
                        (typeof(LeftSprProduceOperation), SystemConstant.DB6004),
                        (typeof(LeftSprStationError), SystemConstant.DB6002),
                        (typeof(LeftSprStationStatus), SystemConstant.DB6000),
                        (typeof(LeftSprProduceStatus), SystemConstant.DB6013),
                    }
                },
                { "leftGlue", new List<(Type, int)>
                    {
                        (typeof(LeftGlueBeat), SystemConstant.DB6001),
                        (typeof(LeftGlueCode), SystemConstant.DB6009),
                        (typeof(LeftGlueDeviceErrorMessage), SystemConstant.DB6006),
                        (typeof(LeftGlueDeviceErrorStatus), SystemConstant.DB6010),
                        (typeof(LeftGluePower), SystemConstant.DB6003),
                        (typeof(LeftGlueProduceOperation), SystemConstant.DB6004),
                        (typeof(LeftGlueStationError), SystemConstant.DB6002),
                        (typeof(LeftGlueStationStatus), SystemConstant.DB6000),
                        (typeof(LeftGlueProduceStatus), SystemConstant.DB6013),
                    }
                },
                { "endWorkSpr", new List<(Type, int)>
                    {
                        (typeof(EndWorkSprBeat), SystemConstant.DB6001),
                        (typeof(EndWorkSprCode), SystemConstant.DB6009),
                        (typeof(EndWorkSprDeviceErrorMessage), SystemConstant.DB6006),
                        (typeof(EndWorkSprDeviceErrorStatus), SystemConstant.DB6010),
                        (typeof(EndWorkSprPower), SystemConstant.DB6003),
                        (typeof(EndWorkSprProduceOperation), SystemConstant.DB6004),
                        (typeof(EndWorkSprStationError), SystemConstant.DB6002),
                        (typeof(EndWorkSprStationStatus), SystemConstant.DB6000),
                        (typeof(EndWorkSprProduceStatus), SystemConstant.DB6013),
                    }
                },
                { "rightSpr", new List<(Type, int)>
                    {
                        (typeof(RightSprBeat), SystemConstant.DB6001),
                        (typeof(RightSprCode), SystemConstant.DB6009),
                        (typeof(RightSprDeviceErrorMessage), SystemConstant.DB6006),
                        (typeof(RightSprDeviceErrorStatus), SystemConstant.DB6010),
                        (typeof(RightSprPower), SystemConstant.DB6003),
                        (typeof(RightSprProduceOperation), SystemConstant.DB6004),
                        (typeof(RightSprStationError), SystemConstant.DB6002),
                        (typeof(RightSprStationStatus), SystemConstant.DB6000),
                        (typeof(RightSprProduceStatus), SystemConstant.DB6013),
                    }
                },
                { "rightGlue", new List<(Type, int)>
                    {
                        (typeof(RightGlueBeat), SystemConstant.DB6001),
                        (typeof(RightGlueCode), SystemConstant.DB6009),
                        (typeof(RightGlueDeviceErrorMessage), SystemConstant.DB6006),
                        (typeof(RightGlueDeviceErrorStatus), SystemConstant.DB6010),
                        (typeof(RightGluePower), SystemConstant.DB6003),
                        (typeof(RightGlueProduceOperation), SystemConstant.DB6004),
                        (typeof(RightGlueStationError), SystemConstant.DB6002),
                        (typeof(RightGlueStationStatus), SystemConstant.DB6000),
                        (typeof(RightGlueProduceStatus), SystemConstant.DB6013),
                    }
                },
                { "studWeld", new List<(Type, int)>
                    {
                        (typeof(StudWeldBeat), SystemConstant.DB6001),
                        (typeof(StudWeldCode), SystemConstant.DB6009),
                        (typeof(StudWeldDeviceErrorMessage), SystemConstant.DB6006),
                        (typeof(StudWeldDeviceErrorStatus), SystemConstant.DB6010),
                        (typeof(StudWeldPower), SystemConstant.DB6003),
                        (typeof(StudWeldProduceOperation), SystemConstant.DB6004),
                        (typeof(StudWeldStationError), SystemConstant.DB6002),
                        (typeof(StudWeldStationStatus), SystemConstant.DB6000),
                        (typeof(StudWeldProduceStatus), SystemConstant.DB6013),
                    }
                },
                { "Clinch01", new List<(Type, int)>
                    {
                        (typeof(Clinch01Beat), SystemConstant.DB6001),
                        (typeof(Clinch01Code), SystemConstant.DB6009),
                        (typeof(Clinch01DeviceErrorMessage), SystemConstant.DB6006),
                        (typeof(Clinch01DeviceErrorStatus), SystemConstant.DB6010),
                        (typeof(Clinch01Power), SystemConstant.DB6003),
                        (typeof(Clinch01ProduceOperation), SystemConstant.DB6004),
                        (typeof(Clinch01StationError), SystemConstant.DB6002),
                        (typeof(Clinch01StationStatus), SystemConstant.DB6000),
                        (typeof(Clinch01ProduceStatus), SystemConstant.DB6013),
                    }
                },
                { "Clinch02", new List<(Type, int)>
                    {
                        (typeof(Clinch02Beat), SystemConstant.DB6001),
                        (typeof(Clinch02Code), SystemConstant.DB6009),
                        (typeof(Clinch02DeviceErrorMessage), SystemConstant.DB6006),
                        (typeof(Clinch02DeviceErrorStatus), SystemConstant.DB6010),
                        (typeof(Clinch02Power), SystemConstant.DB6003),
                        (typeof(Clinch02ProduceOperation), SystemConstant.DB6004),
                        (typeof(Clinch02StationError), SystemConstant.DB6002),
                        (typeof(Clinch02StationStatus), SystemConstant.DB6000),
                        (typeof(Clinch02ProduceStatus), SystemConstant.DB6013),
                    }
                },
                { "Clinch03", new List<(Type, int)>
                    {
                        (typeof(Clinch03Beat), SystemConstant.DB6001),
                        (typeof(Clinch03Code), SystemConstant.DB6009),
                        (typeof(Clinch03DeviceErrorMessage), SystemConstant.DB6006),
                        (typeof(Clinch03DeviceErrorStatus), SystemConstant.DB6010),
                        (typeof(Clinch03Power), SystemConstant.DB6003),
                        (typeof(Clinch03ProduceOperation), SystemConstant.DB6004),
                        (typeof(Clinch03StationError), SystemConstant.DB6002),
                        (typeof(Clinch03StationStatus), SystemConstant.DB6000),
                        (typeof(Clinch03ProduceStatus), SystemConstant.DB6013),
                    }
                },
                { "Clinch04", new List<(Type, int)>
                    {
                        (typeof(Clinch04Beat), SystemConstant.DB6001),
                        (typeof(Clinch04Code), SystemConstant.DB6009),
                        (typeof(Clinch04DeviceErrorMessage), SystemConstant.DB6006),
                        (typeof(Clinch04DeviceErrorStatus), SystemConstant.DB6010),
                        (typeof(Clinch04Power), SystemConstant.DB6003),
                        (typeof(Clinch04ProduceOperation), SystemConstant.DB6004),
                        (typeof(Clinch04StationError), SystemConstant.DB6002),
                        (typeof(Clinch04StationStatus), SystemConstant.DB6000),
                        (typeof(Clinch04ProduceStatus), SystemConstant.DB6013),
                    }
                },
                { "Clinch05", new List<(Type, int)>
                    {
                        (typeof(Clinch05Beat), SystemConstant.DB6001),
                        (typeof(Clinch05Code), SystemConstant.DB6009),
                        (typeof(Clinch05DeviceErrorMessage), SystemConstant.DB6006),
                        (typeof(Clinch05DeviceErrorStatus), SystemConstant.DB6010),
                        (typeof(Clinch05Power), SystemConstant.DB6003),
                        (typeof(Clinch05ProduceOperation), SystemConstant.DB6004),
                        (typeof(Clinch05StationError), SystemConstant.DB6002),
                        (typeof(Clinch05StationStatus), SystemConstant.DB6000),
                        (typeof(Clinch05ProduceStatus), SystemConstant.DB6013),
                    }
                },
                { "FDS01", new List<(Type, int)>
                    {
                        (typeof(FDS01Beat), SystemConstant.DB6001),
                        (typeof(FDS01Code), SystemConstant.DB6009),
                        (typeof(FDS01DeviceErrorMessage), SystemConstant.DB6006),
                        (typeof(FDS01DeviceErrorStatus), SystemConstant.DB6010),
                        (typeof(FDS01Power), SystemConstant.DB6003),
                        (typeof(FDS01ProduceOperation), SystemConstant.DB6004),
                        (typeof(FDS01StationError), SystemConstant.DB6002),
                        (typeof(FDS01StationStatus), SystemConstant.DB6000),
                        (typeof(FDS01ProduceStatus), SystemConstant.DB6013),
                    }
                },
                { "Glue01", new List<(Type, int)>
                    {
                        (typeof(Glue01Beat), SystemConstant.DB6001),
                        (typeof(Glue01Code), SystemConstant.DB6009),
                        (typeof(Glue01DeviceErrorMessage), SystemConstant.DB6006),
                        (typeof(Glue01DeviceErrorStatus), SystemConstant.DB6010),
                        (typeof(Glue01Power), SystemConstant.DB6003),
                        (typeof(Glue01ProduceOperation), SystemConstant.DB6004),
                        (typeof(Glue01StationError), SystemConstant.DB6002),
                        (typeof(Glue01StationStatus), SystemConstant.DB6000),
                        (typeof(Glue01ProduceStatus), SystemConstant.DB6013),
                    }
                },
                { "Spr01", new List<(Type, int)>
                    {
                        (typeof(Spr01Beat), SystemConstant.DB6001),
                        (typeof(Spr01Code), SystemConstant.DB6009),
                        (typeof(Spr01DeviceErrorMessage), SystemConstant.DB6006),
                        (typeof(Spr01DeviceErrorStatus), SystemConstant.DB6010),
                        (typeof(Spr01Power), SystemConstant.DB6003),
                        (typeof(Spr01ProduceOperation), SystemConstant.DB6004),
                        (typeof(Spr01StationError), SystemConstant.DB6002),
                        (typeof(Spr01StationStatus), SystemConstant.DB6000),
                        (typeof(Spr01ProduceStatus), SystemConstant.DB6013),
                    }
                },
                { "Spr02", new List<(Type, int)>
                    {
                        (typeof(Spr02Beat), SystemConstant.DB6001),
                        (typeof(Spr02Code), SystemConstant.DB6009),
                        (typeof(Spr02DeviceErrorMessage), SystemConstant.DB6006),
                        (typeof(Spr02DeviceErrorStatus), SystemConstant.DB6010),
                        (typeof(Spr02Power), SystemConstant.DB6003),
                        (typeof(Spr02ProduceOperation), SystemConstant.DB6004),
                        (typeof(Spr02StationError), SystemConstant.DB6002),
                        (typeof(Spr02StationStatus), SystemConstant.DB6000),
                        (typeof(Spr02ProduceStatus), SystemConstant.DB6013),
                    }
                },
                { "StudWeld01", new List<(Type, int)>
                    {
                        (typeof(StudWeld01Beat), SystemConstant.DB6001),
                        (typeof(StudWeld01Code), SystemConstant.DB6009),
                        (typeof(StudWeld01DeviceErrorMessage), SystemConstant.DB6006),
                        (typeof(StudWeld01DeviceErrorStatus), SystemConstant.DB6010),
                        (typeof(StudWeld01Power), SystemConstant.DB6003),
                        (typeof(StudWeld01ProduceOperation), SystemConstant.DB6004),
                        (typeof(StudWeld01StationError), SystemConstant.DB6002),
                        (typeof(StudWeld01StationStatus), SystemConstant.DB6000),
                        (typeof(StudWeld01ProduceStatus), SystemConstant.DB6013),
                    }
                },
                { "StudWeld02", new List<(Type, int)>
                    {
                        (typeof(StudWeld02Beat), SystemConstant.DB6001),
                        (typeof(StudWeld02Code), SystemConstant.DB6009),
                        (typeof(StudWeld02DeviceErrorMessage), SystemConstant.DB6006),
                        (typeof(StudWeld02DeviceErrorStatus), SystemConstant.DB6010),
                        (typeof(StudWeld02Power), SystemConstant.DB6003),
                        (typeof(StudWeld02ProduceOperation), SystemConstant.DB6004),
                        (typeof(StudWeld02StationError), SystemConstant.DB6002),
                        (typeof(StudWeld02StationStatus), SystemConstant.DB6000),
                        (typeof(StudWeld02ProduceStatus), SystemConstant.DB6013),
                    }
                },
                { "StudWeld03", new List<(Type, int)>
                    {
                        (typeof(StudWeldBeat), SystemConstant.DB6001),
                        (typeof(StudWeldCode), SystemConstant.DB6009),
                        (typeof(StudWeldDeviceErrorMessage), SystemConstant.DB6006),
                        (typeof(StudWeldDeviceErrorStatus), SystemConstant.DB6010),
                        (typeof(StudWeldPower), SystemConstant.DB6003),
                        (typeof(StudWeldProduceOperation), SystemConstant.DB6004),
                        (typeof(StudWeldStationError), SystemConstant.DB6002),
                        (typeof(StudWeldStationStatus), SystemConstant.DB6000),
                        (typeof(StudWeldProduceStatus), SystemConstant.DB6013),
                    }
                }
            };

            // 定义读取数据并添加到字典的方法
            void AddInstance(Type type, int address)
            {
                // 引用ReadClass方法的命名空间和类
                var method = typeof(Plc).GetMethod("ReadClass", new Type[] { typeof(int), typeof(int) })?.MakeGenericMethod(type);
                var instance = method.Invoke(plc, new object[] { address, 0 });
                dic.Add(instance, type);
            }

            try
            {
                // 检查并处理对应的Name类型
                if (typeAddressMap.ContainsKey(plcName))
                {
                    foreach (var (type, address) in typeAddressMap[plcName])
                    {
                        AddInstance(type, address);
                    }
                }

                return dic;
            }
            catch (Exception ex)
            {
                Tools.ProduceLogTextError($"{plcName}获取Plc数据实例与实例类型映射失败，原因是：" + ex.Message);
            }

            return dic;
        }


        /// <summary>
        /// 偏移量映射表
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static (int, int) GetOff(string name)
        {
            Dictionary<string, Dictionary<int, int>> dic = new Dictionary<string, Dictionary<int, int>>();
            int count = 0;
            int outerKey = 0;
            int innerValue = 0;
            string key = "";
            if (name.Contains("ErrorStatus"))
            {
                //处理报警信息
                count = 40;
                outerKey = 0;
                innerValue = 0;
                key = "ErrorStatus";
            }

            // 初始化映射表
            for (int i = 1; i <= count; i++)
            {
                string keys = key + i; // 生成键名
                Dictionary<int, int> innerDic = new Dictionary<int, int>
                {
                    { outerKey, innerValue } // 添加初始值
                };

                // 将innerDic添加到dic
                dic[keys] = innerDic;

                // 递增内层字典的值
                innerValue++;

                // 检查是否需要更新outerKey和重置innerValue
                if (innerValue == 8)
                {
                    innerValue = 0; // 重置为0
                    outerKey++; // 增加outerKey
                }
            }

            //获取对应数据
            foreach (var item in dic)
            {
                if (name.Equals(item.Key))
                {
                    foreach (var item1 in item.Value)
                    {
                        return (item1.Key, item1.Value);
                    }
                }
            }
            return (10, 10);
        }

        /// <summary>
        /// 获取Cpu唯一标识
        /// </summary>
        public static string DeviceIdentifier()
        {
            string cpuId = string.Empty;
            using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("select ProcessorId from Win32_Processor"))
            {
                foreach (ManagementObject obj in searcher.Get())
                {
                    cpuId = obj["ProcessorId"].ToString();
                    break;
                }
            }
            return cpuId;
        }

        /// <summary>
        /// 设备与数据库账号映射表
        /// </summary>
        /// <param name="cpuIp"></param>
        /// <returns></returns>
        public static (string, string) GetMySqlUser(string cpuIp)
        {
            string userName = "";
            string passWord = "";
            //BFEBFBFF000906EA
            //BFEBFBFF000906EA
            Dictionary<string, Dictionary<string, string>> dic = new Dictionary<string, Dictionary<string, string>>()
            {
                {"BFEBFBFF000906EA",new Dictionary<string, string>()
                {
                    { "root","yk19991223"}
                } },
                {"BFEBFBFF000806EB",new Dictionary<string, string>()
                {
                    { "root","265422"}
                } }
            };

            foreach (var item in dic)
            {
                if (item.Key.Equals(cpuIp))
                {
                    //解析Value
                    foreach (var item1 in item.Value)
                    {
                        userName = item1.Key;
                        passWord = item1.Value;
                    }
                }
            }
            userName = string.IsNullOrEmpty(userName) ? SystemConstant.MySqlName : userName;
            passWord = string.IsNullOrEmpty(passWord) ? SystemConstant.MySqlPassWord : passWord;
            return (userName, passWord);
        }

        /// <summary>
        /// 请求对照表
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string UrlAnalysis(string url)
        {
            Dictionary<string, string> dic
                = new Dictionary<string, string>()
                {
                    {"OrderInfo","ProcessRequest" },
                    {"SpotCheckOk","SpotCheckOk" },
                    {"OrderEnd","OrderEnd" },
                    {"AdditionalOrder","AdditionalOrder" },
                    {"ISVGlue","ISVGlue" },
                    {"GetNgCode","GetNgCode" },
                    {"AndonTTS","AndonTTS" },
                    {"AndonDelete","AndonDelete" },
                    {"ReportingForWork","ReportingForWork" },
                    {"ReceiveReport","ReceiveReport" },
                    {"CheckReport","CheckReport" },
                };
            foreach (var item in dic)
            {
                if (url.Contains(item.Key))
                {
                    return item.Value;
                }
            }
            return "";
        }

        /// <summary>
        /// 线体编码与PlcName的映射
        /// </summary>
        /// <returns></returns>
        public static string GetTasksType(string lineName)
        {
            Dictionary<string, string> taskList = new Dictionary<string, string>()
            {
                { "305","leftSpr"},
                { "306","leftGlue"},
                { "307","endWorkSpr"},
                { "308","rightSpr"},
                { "309","rightGlue"},
                { "310","studWeld"},
            };
            return taskList.TryGetValue(lineName, out string plcName) ? plcName : "";
        }

        /// <summary>
        /// 根据年份获取标识
        /// </summary>
        /// <param name="year"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentException"></exception>
        public static string GetYear(int year)
        {
            // 定义一个年份到字母的映射
            var yearToLetterMap = new Dictionary<int, string>
            {
                { 2024, "R" },
                { 2025, "S" },
                { 2026, "T" },
                { 2027, "V" },
                { 2028, "W" },
                { 2029, "X" },
                { 2030, "Y" }
            };

            // 检查年份是否在映射中
            if (yearToLetterMap.TryGetValue(year, out string letter))
            {
                return letter;
            }

            // 如果年份未定义，返回一个默认值或抛出异常
            throw new ArgumentException($"未定义年份 {year} 的对应字母。");
        }

        /// <summary>
        /// 根据月份获取标识
        /// </summary>
        /// <param name="month"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentException"></exception>
        public static string GetMonth(int month)
        {
            // 检查输入是否为有效月份
            if (month < 1 || month > 12)
            {
                throw new ArgumentException("月份必须在 1 到 12 之间。");
            }

            // 根据月份返回对应的字符串
            if (month >= 1 && month <= 9)
            {
                return month.ToString(); // 1 到 9 返回数字字符串
            }

            switch (month)
            {
                case 10:
                    return "A"; // 十月对应 A
                case 11:
                    return "B"; // 十一月对应 B                                                                                                   
                case 12:
                    return "C"; // 十二月对应 C
                default:
                    throw new ArgumentException("无效的月份。");
            }
        }

        /// <summary>
        /// 根据天获取标识
        /// </summary>
        /// <param name="day"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentException"></exception>
        public static string GetDay(int day)
        {
            // 检查输入是否为有效日期
            if (day < 1 || day > 31)
            {
                throw new ArgumentException("日期必须在 1 到 31 之间。");
            }

            // 1 到 9 返回数字字符串
            if (day >= 1 && day <= 9)
            {
                return day.ToString();
            }

            // 10 到 31 映射到字母，跳过 I、O、Q 和 U
            char[] letters = "ABCDEFGHJKLMNPRSTVWXYZ".ToCharArray(); // 排除 I, O, Q, U
            int index = day - 10; // 计算映射到字母数组的索引

            if (index < letters.Length)
            {
                return letters[index].ToString();
            }

            throw new ArgumentException($"日期 {day} 超出字母映射范围。");
        }

        /// <summary>
        /// 安灯故障对照表
        /// </summary>
        /// <param name="deviceId"></param>
        /// <param name="faultMessage"></param>
        /// <returns></returns>
        public static bool DeviceIdInAndon(string deviceId, string faultMessage)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>()
            {
                { "",""},
                { "",""},
                { "",""},
                { "",""},
                { "",""},
            };

            if (dic.TryGetValue(deviceId, out string message).Equals(faultMessage))
                return true;
            return false;
        }

        /// <summary>
        /// 通过工序编号获取点焊机器人对应编号
        /// </summary>
        /// <param name="operaiton"></param>
        /// <returns></returns>
        public static List<string> GetRobotByOperation(string operaiton)
        {
            Dictionary<string, List<string>> dic = new Dictionary<string, List<string>>
            {
                { "L1",new List<string> { "52" } },
                { "L2",new List<string> { "53" } },
                { "L3",new List<string> { "54" } },
                { "L4",new List<string> { "52" } },
                { "L5",new List<string> { "53" } },
                { "L6",new List<string> { "54" } },
                { "L7",new List<string> { "55" } },
                { "L8",new List<string> { "55" } },
                { "L11",new List<string> { "57","58" } },
                { "L12",new List<string> { "57","58" } },
                { "L13",new List<string> { "59" } },
                { "L14",new List<string> { "60" } },
                { "R1",new List<string> { "63" } },
                { "R2",new List<string> { "64" } },
                { "R3",new List<string> { "63" } },
                { "R4",new List<string> { "64" } },
                { "R5",new List<string> { "66" } },
                { "R6",new List<string> { "66" } },
                { "R9",new List<string> { "67" } },
                { "R10",new List<string> { "68","69" } },
                { "R11",new List<string> { "68", "69" } },
                { "R12",new List<string> { "70" } },
                { "R13",new List<string> { "71" } },
                {"SC12-1",new List<string>{"36"}},
                {"SC12-2",new List<string>{"36"}},
                {"SC10-1",new List<string>{"37"}},
                {"SC10-3",new List<string>{"37"}},
                {"SC8-1",new List<string>{"38"}},
                {"SC8-2",new List<string>{"38"}},
                {"SC9-1",new List<string>{"38"}},
                {"SC11-1",new List<string>{"38"}},
            };
            if (dic.TryGetValue(operaiton, out var value))
            {
                return value;
            }
            else
            {
                return new List<string>();
            }
        }

        /// <summary>
        /// 根据工厂下发工序号获取对应线体中存储的工序号
        /// </summary>
        /// <param name="operation"></param>
        /// <param name="MaterialCode"></param>
        /// <returns></returns>
        public static (string, bool) GetOperation(string operation, string MaterialCode)
        {
            // CQYQ-B04340004-01-RSW
            Dictionary<string, Dictionary<string, string>> dic = new Dictionary<string, Dictionary<string, string>>()
            {
                { "leftSpr",new Dictionary<string, string>{
                    { "CQYQ-B08330001-01-SPR", "Op10_A" },
                    { "CQYQ-B08330001-02-SPR", "Op10_B" }
                } },

                { "leftGlue",new Dictionary<string, string>{
                    { "CQYQ-B14330003-01-GLU", "Op10_1" },
                    { "CQYQ-B14330002-01-GLU", "Op10_2" },
                    { "CQYQ-B14330001-01-GLU", "Op20" },
                } },
                { "endWorkSpr",new Dictionary<string, string>{
                    { "CQYQ-C3319-01-SPR", "Op10_A,Op10_B" },
                } },
                { "rightSpr",new Dictionary<string, string>{
                    { "CQYQ-B08330002-01-SPR", "Op10_A,Op10_B" },
                } },
                { "rightGlue",new Dictionary<string, string>{
                    { "CQYQ-B14330004-01-GLU", "Op10_1,Op20_1" },
                    { "CQYQ-B14330005-01-GLU", "Op10_2,Op20_2" },
                    { "CQYQ-B14330006-01-GLU", "Op10_3" },
                } },
                { "studWeld",new Dictionary<string, string>{
                    { "CQYQ-B06330018-01-STU", "Op10_1,Op20_1" },
                    { "CQYQ-B06330019-01-STU", "Op20_2" },
                } },


                { "FDS01",new Dictionary<string, string>{
                    { "", "Op10" },
                    { "", "Op20" },
                } },
                { "Glue01",new Dictionary<string, string>{
                    { "", "Op10_1" },
                    { "", "Op10_2" },
                    { "", "Op10_3" },
                    { "", "Op10_4" },
                    { "", "Op20_1" },
                    { "", "Op20_2" },
                } },
                { "Spr01",new Dictionary<string, string>{
                    { "", "Op10" },
                    { "", "Op20" },
                } },
                { "Spr02",new Dictionary<string, string>{
                    { "", "Op10_1,Op20_1" },
                    { "", "Op20_2" },
                } },
                { "Clinch01",new Dictionary<string, string>{
                    { "", "Op10" },
                    { "", "Op20" },
                } },
                { "Clinch02",new Dictionary<string, string>{
                    { "", "Op10" },
                    { "", "Op20" },
                } },
                { "Clinch03",new Dictionary<string, string>{
                    { "", "Op10" },
                    { "", "Op20" },
                    { "", "Op30" },
                } },
                { "Clinch04",new Dictionary<string, string>{
                    { "", "Op10" },
                    { "", "Op20" },
                } },
                { "Clinch05",new Dictionary<string, string>{
                    { "", "Op10" },
                    { "", "Op20" },
                } },
                { "StudWeld01",new Dictionary<string, string>{
                    { "", "Op10" },
                    { "", "Op20" },
                    { "", "Op30" },
                    { "", "Op40" },
                    { "", "Op50" },
                    { "", "Op60" },
                    { "", "Op70" },
                } },
                { "StudWeld02",new Dictionary<string, string>{
                    { "", "Op10" },
                    { "", "Op20" },
                    { "", "Op30" },
                    { "", "Op40" },
                    { "", "Op50" },
                    { "", "Op60" },
                    { "", "Op70" },
                    { "", "Op80" },
                    { "", "Op90" },
                    { "", "Op100" },
                } },
                { "StudWeld03",new Dictionary<string, string>{
                    { "", "Op10" },
                    { "", "Op20" },
                    { "", "Op30" },
                } },
            };
            if (dic.TryGetValue(MaterialCode, out Dictionary<string, string> operationDic))
                if (operationDic.TryGetValue(operation, out string value))
                    return (value, true);
            return (operation, false);
        }


        /// <summary>
        /// 根据plcName获取该Plc存在的最大工单数量
        /// </summary>
        /// <param name="plcName"></param>
        /// <returns></returns>
        public static int GetCount(string plcName)
        {
            Dictionary<string, int> dic = new Dictionary<string, int>()
            {
                {"leftSpr",1 },
                {"leftGlue",3 },
                {"endWorkSpr",1 },
                {"rightSpr",1 },
                {"rightGlue",3 },
                {"studWeld",2 },

                {"FDS01",2 },
                {"Glue01",6 },
                {"Spr01",2 },
                {"Spr02",3 },
                {"Clinch01",2 },
                {"Clinch02",2 },
                {"Clinch03",3 },
                {"Clinch04",2 },
                {"Clinch05",2 },
                {"StudWeld01",7 },
                {"StudWeld02",10 },
                {"StudWeld03",3 },
            };
            if (dic.TryGetValue(plcName, out int value))
            {
                return value;
            }
            return 0;
        }

        /// <summary>
        /// 根据不同零件号补全自动工序
        /// </summary>
        /// <param name="processCodes"></param>
        /// <returns></returns>
        public static List<string> AddOperation(List<string> operations, List<string> processCodes, string plcName)
        {
            Dictionary<string, List<string>> dic = new Dictionary<string, List<string>>()
            {
            };

            if (dic.TryGetValue(plcName, out List<string> value))
            {
                operations.AddRange(value);
            }
            operations.AddRange(processCodes);
            return operations;
        }

        public static bool MapRobot(string robot)
        {
            List<string> robots = new List<string>()
            {
                "52",
                "53",
                "55",
                "66",
                "63",
                "64"
            };
            return robots.Contains(robot);
        }

        /// <summary>
        /// 获取读取信号偏移量
        /// </summary>
        /// <param name="plcName"></param>
        /// <param name="operationReal"></param>
        /// <returns></returns>
        public static (int startByteAdr, int bitAdr) OperationGetReadOff(string plcName, string operation)
        {
            var dataDict = new Dictionary<string, Dictionary<string, Tuple<int, int>>>();

            dataDict["leftSpr"] = new Dictionary<string, Tuple<int, int>>
            {
                { "Op10_A", new Tuple<int, int>(0, 0) },
                { "Op10_B", new Tuple<int, int>(0, 1) },
            };

            dataDict["leftGlue"] = new Dictionary<string, Tuple<int, int>>
            {
                { "Op10_1", new Tuple<int, int>(0, 0) },
                { "Op10_2", new Tuple<int, int>(0, 1) },
                { "Op20", new Tuple<int, int>(0, 2) },
            };

            dataDict["endWorkSpr"] = new Dictionary<string, Tuple<int, int>>
            {
                { "Op10_A", new Tuple<int, int>(0, 0) },
                { "Op10_B", new Tuple<int, int>(0, 1) },
            };

            dataDict["rightSpr"] = new Dictionary<string, Tuple<int, int>>
            {
                { "Op10_A", new Tuple<int, int>(0, 0) },
                { "Op10_B", new Tuple<int, int>(0, 1) },
            };

            dataDict["rightGlue"] = new Dictionary<string, Tuple<int, int>>
            {
                { "Op10_1", new Tuple<int, int>(0, 0) },
                { "Op10_2", new Tuple<int, int>(0, 1) },
                { "Op10_3", new Tuple<int, int>(0, 2) },
                { "Op20_1", new Tuple<int, int>(0, 3) },
                { "Op20_2", new Tuple<int, int>(0, 4) },
            };

            dataDict["studWeld"] = new Dictionary<string, Tuple<int, int>>
            {
                { "Op10_1", new Tuple<int, int>(0, 0) },
                { "Op20_1", new Tuple<int, int>(0, 1) },
                { "Op20_2", new Tuple<int, int>(0, 2) },
            };

            if (dataDict.ContainsKey(plcName) && dataDict[plcName].ContainsKey(operation))
            {
                var result = dataDict[plcName][operation];
                return (result.Item1, result.Item2);
            }

            return (10, 10);
        }

        /// <summary>
        /// 判断是否是下线工序
        /// </summary>
        /// <param name="operationItem"></param>
        /// <returns></returns>
        public static bool GetLineIsEnd(string plcName, string operationItem)
        {
            Dictionary<string, List<string>> dic = new Dictionary<string, List<string>>();

            dic["leftSpr"] = new List<string>()
            {
                "Op10_B",
            };

            dic["leftGlue"] = new List<string>()
            {
                "Op10_1",
                "Op10_2",
                "Op20",
            };

            dic["endWorkSpr"] = new List<string>()
            {
                "Op10_A",
                "Op10_B",
            };

            dic["rightSpr"] = new List<string>()
            {
                "Op10_A",
                "Op10_B",
            };

            dic["rightGlue"] = new List<string>()
            {
                "Op10_1",
                "Op10_2",
                "Op10_3",
                "Op20_1",
                "Op20_2",
            };

            dic["studWeld"] = new List<string>()
            {
                "Op10_1",
                "Op20_1",
                "Op20_2",
            };


            if (dic.TryGetValue(plcName, out List<string> operation))
            {
                if (operation.Contains(operationItem))
                    return true;
            }
            return false;
        }

        /// <summary>
        /// 根据线体号(任务号)与工序获取反馈Plc的偏移量
        /// </summary>
        /// <param name="plcName"></param>
        /// <param name="operation"></param>
        /// <returns></returns>
        public static (int, int) OperationGetOff(string plcName, string operation)
        {
            var dataDict = new Dictionary<string, Dictionary<string, Tuple<int, int>>>();

            dataDict["leftSpr"] = new Dictionary<string, Tuple<int, int>>
            {
                { "Op10_A", new Tuple<int, int>(2, 4) },
                { "Op10_B", new Tuple<int, int>(2, 5) },
            };

            dataDict["leftGlue"] = new Dictionary<string, Tuple<int, int>>
            {
                { "Op10_1", new Tuple<int, int>(2, 4) },
                { "Op10_2", new Tuple<int, int>(2, 5) },
                { "Op20", new Tuple<int, int>(2, 6) },
            };

            dataDict["endWorkSpr"] = new Dictionary<string, Tuple<int, int>>
            {
                { "Op10_A", new Tuple<int, int>(2, 4) },
                { "Op10_B", new Tuple<int, int>(2, 5) },
            };

            dataDict["rightSpr"] = new Dictionary<string, Tuple<int, int>>
            {
                { "Op10_A", new Tuple<int, int>(2, 4) },
                { "Op10_B", new Tuple<int, int>(2, 5) },
            };

            dataDict["rightGlue"] = new Dictionary<string, Tuple<int, int>>
            {
                { "Op10_1", new Tuple<int, int>(2, 4) },
                { "Op10_2", new Tuple<int, int>(2, 5) },
                { "Op10_3", new Tuple<int, int>(2, 6) },
                { "Op20_1", new Tuple<int, int>(2, 7) },
                { "Op20_2", new Tuple<int, int>(3, 0) },
            };

            dataDict["studWeld"] = new Dictionary<string, Tuple<int, int>>
            {
                { "Op10_1", new Tuple<int, int>(2, 4) },
                { "Op20_1", new Tuple<int, int>(2, 5) },
                { "Op20_2", new Tuple<int, int>(2, 6) },
            };

            if (dataDict.ContainsKey(plcName) && dataDict[plcName].ContainsKey(operation))
            {
                var result = dataDict[plcName][operation];
                return (result.Item1, result.Item2);
            }

            return (10, 10);
        }

        /// <summary>
        /// 根据物料编码获取下发位置索引
        /// </summary>
        /// <param name="materialCode"></param>
        /// <returns></returns>
        public static int GetOrderCount(string plcName, string materialCode)
        {
            Dictionary<string, Dictionary<string, int>> dic = new Dictionary<string, Dictionary<string, int>>();

            dic["leftSpr"] = new Dictionary<string, int>()
            {
                {"B08330001", 1},
            };

            dic["leftGlue"] = new Dictionary<string, int>()
            {
                {"B14330001", 1},
                {"B14330002", 2},
                {"B14330003", 3},
            };

            dic["endWorkSpr"] = new Dictionary<string, int>()
            {
                {"C33000019", 1},
            };

            dic["rightSpr"] = new Dictionary<string, int>()
            {
                {"B08330002", 1},
            };

            dic["rightGlue"] = new Dictionary<string, int>()
            {
                {"B14330004", 1},
                {"B14330006", 2},
                {"B14330005", 3},
            };

            dic["studWeld"] = new Dictionary<string, int>()
            {
                {"B07330002", 1},
                {"B05330002", 2},
            };



            dic["SubFDS_1"] = new Dictionary<string, int>()
            {
                {"2", 1},
                {"3", 2},
            };

            dic["SubGlue_1"] = new Dictionary<string, int>()
            {
                // R20 21 - M1 M2
                {"1", 1},
                {"2", 2},
                {"3", 3},
                {"4", 4},
                {"5", 5},
                {"6", 6},
            };

            dic["SubSpr_1"] = new Dictionary<string, int>()
            {
                // R22 23 - M1 M2
                {"C33000002", 1},
                {"C33000011", 2},
            };

            dic["SubSpr_2"] = new Dictionary<string, int>()
            {
                // R24 25 - M1 M2 R26 - M1 M2
                {"C33000005", 1},
                {"C33000003", 2},
                {"C33000004", 3},
            };

            dic["SubClinch_1"] = new Dictionary<string, int>()
            {
                // Y1
                {"C33000007", 1},
                {"C33000008", 2},
            };

            dic["SubClinch_2"] = new Dictionary<string, int>()
            {
                // Y2
                {"C33000006", 1},
                {"C33000009", 2},
            };

            dic["SubClinch_3"] = new Dictionary<string, int>()
            {
                // Y3
                {"B05330009", 1},
                {"C33000009", 2},
                {"C33000006", 3},
            };

            dic["SubClinch_4"] = new Dictionary<string, int>()
            {
                // Y4
                {"B05330006", 1},
                {"B05330007", 2},
            };

            dic["SubClinch_5"] = new Dictionary<string, int>()
            {
                // Y5
                {"B05330006?", 1},
                {"B05330007?", 2},
            };

            dic["SubStudWeld_1"] = new Dictionary<string, int>()
            {
                // R27 - M1 M2
                {"B06330007", 1},
                {"B06330005", 2},
                {"B06330006", 3},
                {"B06330008", 4},
                {"B06330003", 5},
                {"B06330002", 6},
                {"B06330010", 7},
            };

            dic["SubStudWeld_2"] = new Dictionary<string, int>()
            {
                // R28 - M1 M2 R29 - M1 M2
                {"B06330007", 1},
                {"B06330005", 2},
                {"B06330006", 3},
                {"B06330008", 4},
                {"B06330003", 5},
                {"B06330002", 6},
                {"B06330010", 7},
                {"B06330010", 8},
                {"B06330010", 9},
                {"B06330010", 10},
            };

            dic["SubStudWeld_3"] = new Dictionary<string, int>()
            {
                // R30 - M1 M2
                {"B06330009", 1},
                {"B06330001", 2},
                {"C33000010", 3},
            };

            if (dic.TryGetValue(materialCode, out Dictionary<string,int> value))
                return value[materialCode];
            return 10;
        }
    }
}
